﻿/*
	Copyright (C) 2009 - 2012
	WebSite:	Http://wangking717.javaeye.com/
	Author:		wangking
*/  
$(function(){
	var xOffset = -10; // x distance from mouse
    var yOffset = 20; // y distance from mouse  
	
	var _pathName = document.location.pathname;
	var _index = _pathName.substr(1).indexOf("/");
	var _contextPath = _pathName.substr(0,_index+1); //上下文路径
	
	//input action
	/*
	$("[reg],[url]:not([reg]),[tip]").hover(
		function(e) {
			if($(this).attr('tip') != undefined){
				var top = (e.pageY + yOffset);
				var left = (e.pageX + xOffset);
				$('body').append( '<p id="vtip"><img id="vtipArrow" src="/affairs/js/extends/easyvalidator/images/vtip_arrow.png"/>' + $(this).attr('tip') + '</p>' );
				$('p#vtip').css("top", top+"px").css("left", left+"px");
			}
		},
		function() {
			if($(this).attr('tip') != undefined){
				$("p#vtip").remove();
			}
		}
	).mousemove(
		function(e) {
			if($(this).attr('tip') != undefined){
				var top = (e.pageY + yOffset);
				var left = (e.pageX + xOffset);
				$("p#vtip").css("top", top+"px").css("left", left+"px");
			}
		}
	).bind("keyup blur",function(){
		if($(this).attr("reg") != undefined){
			validate($(this));
		}else if($(this).attr("url") != undefined){
			ajax_validate($(this));
		}
	});
	*/
	$("[reg],[url]:not([reg]),[tip]").off().on('mouseover mouseout mousemove keyup focusout',function(e){
		 if (e.type == 'mouseover') {
			if($(this).attr('tip') != undefined){
				var top = (e.pageY + yOffset);
				var left = (e.pageX + xOffset);
				$('body').append( '<p class="vtip"><img id="vtipArrow" src="'+_contextPath+'/js/extends/easyvalidator/images/vtip_arrow.png" /><span>' + $(this).attr('tip') + '</span></p>' );
				$('p.vtip').css("top", top+"px").css("left", left+"px");
			}
	  	 };
	  	 if (e.type == 'mouseout') {
			if($(this).attr('tip') != undefined){
				$("p.vtip").remove();
			}
	  	 };
	  	 if (e.type == 'mousemove') {
			if($(this).attr('tip') != undefined){
				var top = (e.pageY + yOffset);
				var left = (e.pageX + xOffset);
				$("p.vtip").css("top", top+"px").css("left", left+"px");
			}
	  	 };
	  	 if (e.type == 'keyup' || e.type == 'focusout') {
			if($(this).attr("reg") != undefined){
				validate($(this));
			}else if($(this).attr("url") != undefined){
				ajax_validate($(this));
			}
	  	 };
	});
	
	
	$("form").submit(function(){
		var isSubmit = true;
		var isExtendsValidateCheck = true;
		$(this).find("[reg]:not(:hidden),[url]:not([reg])").each(function(){
			if($(this).attr("reg") == undefined){
				if(!ajax_validate($(this))){
					isSubmit = false;
				}
			}else{
				if(!validate($(this))){
					isSubmit = false;
				}
			}
		});
		if(typeof(isExtendsValidate) != "undefined"){
   			if(isExtendsValidate){
   				isExtendsValidateCheck = extendsValidate();
			}
   		}
		return isSubmit && isExtendsValidateCheck;
	});
	
});

function validate(obj){
	var reg = new RegExp(obj.attr("reg"));
	var objValue = obj.val();
	
	//qlc ^.+的正则如果全是空白字符可以匹配，但不符合业务规则，所有验证之前先trim一下，去掉收尾的空白字符
	objValue = $.trim(objValue);
	
	if(!reg.test(objValue)){
		change_error_style(obj,"add");
		change_tip(obj,null,"remove");
		return false;
	}else{
		if(obj.attr("url") == undefined){
			change_error_style(obj,"remove");
			change_tip(obj,null,"remove");
			return true;
		}else{
			return ajax_validate(obj);
		}
	}
}

function ajax_validate(obj){
	
	var url_str = obj.attr("url");
	if(url_str.indexOf("?") != -1){
		url_str = url_str+"&"+obj.attr("name")+"="+obj.attr("value");
	}else{
		url_str = url_str+"?"+obj.attr("name")+"="+obj.attr("value");
	}
	var feed_back = $.ajax({url: url_str,cache: false,async: false}).responseText;
	feed_back = feed_back.replace(/(^\s*)|(\s*$)/g, "");
	if(feed_back == 'success'){
		change_error_style(obj,"remove");
		change_tip(obj,null,"remove");
		return true;
	}else{
		change_error_style(obj,"add");
		change_tip(obj,feed_back,"add");
		return false;
	}
}

function change_tip(obj,msg,action_type){
	
	if(obj.attr("tip") == undefined){//初始化判断TIP是否为空
		obj.attr("is_tip_null","yes");
	}
	if(action_type == "add"){
		if(obj.attr("is_tip_null") == "yes"){
			obj.attr("tip",msg);
		}else{
			if(msg != null){
				if(obj.attr("tip_bak") == undefined){
					obj.attr("tip_bak",obj.attr("tip"));
				}
				obj.attr("tip",msg);
				change_tipdiv_text(msg); //qlc
			}
		}
	}else{
		if(obj.attr("is_tip_null") == "yes"){
			obj.removeAttr("tip");
			obj.removeAttr("tip_bak");
		}else{
			obj.attr("tip",obj.attr("tip_bak"));
			change_tipdiv_text(obj.attr("tip_bak")); //qlc
			obj.removeAttr("tip_bak");
		}
	}
}

function change_error_style(obj,action_type){
	if(action_type == "add"){
		obj.addClass("input_validation-failed");
	}else{
		obj.removeClass("input_validation-failed");
	}
}

$.fn.validate_callback = function(msg,action_type,options){
	this.each(function(){
		if(action_type == "failed"){
			change_error_style($(this),"add");
			change_tip($(this),msg,"add");
		}else{
			change_error_style($(this),"remove");
			change_tip($(this),null,"remove");
		}
	});
};


/*
qlc add 20120717
对reg的dom进行验证 通过返回true 失败返回false
*/
$.fn.extend({
	validate_dom:function(){
		var flag = true;
		var isExtendsValidateCheck = true;
		var $regInput = $(this).find('[reg]');
		$regInput.each(function(){
			if(!$(this).is(':hidden')){
				if(!validate($(this))){
					if(flag){
						flag = false;
					}
				}
			}
		});
		
		if(typeof(isExtendsValidate) != "undefined"){
   			if(isExtendsValidate){
   				isExtendsValidateCheck = extendsValidate();
			}
   		}
		
		$regInput.filter('.input_validation-failed').first().focus();//光标定位
		return flag && isExtendsValidateCheck;
	}
});

/*
 qlc add 20120728
 改变tipdiv中提示的文字
*/
function change_tipdiv_text(tipStr){
	var $tipDiv = $("p.vtip");
	if($tipDiv.length != 0 ){
		$tipDiv.find('span').text(tipStr);
	}
}
